Page({

  /**
   * 页面的初始数据
   */
  data: {
    delBtnWidth: 180,
    text:123456,
    id:wx.getStorageSync('id'),
    name:'',
    startX:0,
    startY:0
  },
  change_zl:function(){
    wx: wx.showActionSheet({
      itemList: ['命理资料', '居家资料', '个人起名', '企业资料'],
      success: function (res) {
        if (res.tapIndex == 0) {
          wx:wx.redirectTo({
            url: '../mingli/mingli',
          })
        }else if(res.tapIndex == 1){
            wx: wx.redirectTo({
              url: '../jujia/jujia',
            })
        }else if(res.tapIndex == 2){
            wx: wx.redirectTo({
              url: '../mingzi/mingzi',
            })
        }else{
          wx: wx.redirectTo({
            url: '../qiye/qiye',
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  zl_xq:function(e){
    var type = e.target.dataset.type
    var id = e.target.dataset.id
    if(type == '命理'){
      wx, wx.redirectTo({
        url: '../mingli_upd/mingli_upd?id='+id,
      })
    }else if(type == '起名'){
      wx, wx.redirectTo({
        url: '../mingzi_upd/mingzi_upd?id=' + id,
      })
    }else if(type == '居家'){
      wx, wx.redirectTo({
        url: '../jujia_upd/jujia_upd?id=' + id,
      })
    }else if(type == '企业'){
      wx, wx.redirectTo({
        url: '../qiye_upd/qiye_upd?id=' + id,
      })
    }
  },
  url2:function(){
    wx.redirectTo({
      url: '../home/home',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = wx.getStorageSync('id')
    if (id == '') {
      wx: wx.showToast({
        title: '请先登录',
        icon: 'loading',
        duration: 10000,
        success: function (res) {
          wx.redirectTo({
            url: '../user/user',
          })
        },
      })
    }
    var that = this
    wx.request({
      url: 'https://wx.gjzy.com/weixin/Admin/Index/zlFind',
      data:{
        id:wx.getStorageSync('id')
      },
      success:function(res){
        that.setData({
          name:res.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },
  touchS: function (e) {
    if (e.touches.length == 1) {
      this.setData({
        //设置触摸起始点水平方向位置
        startX: e.touches[0].clientX
      });
    }
  },
  touchM: function (e) {
    if (e.touches.length == 1) {
      //手指移动时水平方向位置
      var moveX = e.touches[0].clientX;
      //手指起始点位置与移动期间的差值
      var disX = this.data.startX - moveX;
      var delBtnWidth = this.data.delBtnWidth;
      console.log(delBtnWidth)
      console.log(this.data)
      var txtStyle = "";
      if (disX == 0 || disX < 0) {//如果移动距离小于等于0，文本层位置不变
        txtStyle = "left:0px";
      } else if (disX > 0) {//移动距离大于0，文本层left值等于手指移动距离
        txtStyle = "left:-" + disX + "px";
        if (disX >= delBtnWidth) {
          //控制手指移动距离最大值为删除按钮的宽度
          txtStyle = "left:-" + delBtnWidth + "px";
        }
      }
      //获取手指触摸的是哪一项
      var index = e.target.dataset.index;
      var name = this.data.name;
      name[index].txtStyle = txtStyle;
      //更新列表的状态
      this.setData({
        name:name
      });
    }
  },
  touchE: function (e) {
    if (e.changedTouches.length == 1) {
      //手指移动结束后水平位置
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束，手指移动的距离
      var disX = this.data.startX - endX;
      var delBtnWidth = this.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮
      var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px";
      //获取手指触摸的是哪一项
      var index = e.target.dataset.index;
      var name = this.data.name;
      console.log(name)
      name[index].txtStyle = txtStyle;
      //更新列表的状态
      this.setData({
        name: name
      });
    }
  },
  //获取元素自适应后的实际宽度
  getEleWidth: function (w) {
    var real = 0;
    try {
      var res = wx.getSystemInfoSync().windowWidth;
      var scale = (750 / 2) / (w / 2);//以宽度750px设计稿做宽度的自适应
      // console.log(scale);
      real = Math.floor(res / scale);
      return real;
    } catch (e) {
      return false;
      // Do something when catch error
    }
  },
  initEleWidth: function () {
    var delBtnWidth = this.getEleWidth(this.data.delBtnWidth);
    this.setData({
      delBtnWidth: delBtnWidth
    });
  },
  //点击删除按钮事件
  delItem: function (e) {
    var that = this
    //获取列表中要删除项的下标
    var index = e.target.dataset.index;
    var id = e.target.dataset.id;
    var type = e.target.dataset.type;
    var name = this.data.name;
    //移除列表中下标为index的项
    console.log(id)
    console.log(type)
    name.splice(index, 1);
    //更新列表的状态
    this.setData({
      name: name
    });
    wx.request({
      url: 'https://wx.gjzy.com/weixin/Admin/Index/zl_delete',
      data:{
        id:id,
        type:type
      },
      method:'GET',
      success:function(res){
        console.log(res)
        if(res.data.state == 0){
          wx.showToast({
            title: res.data.msg,
            icon:'success',
            duration:2000,
          })
        }else if(res.data.state == 1){
          wx.showToast({
            title: res.data.msg,
          })
        }else if(res.data.state == 3){
          wx.showToast({
            title: res.data.msg,
          })
        }
      }
    })
  },
})